package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000117622495";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCVS0Y/cHbvFHpMJHileyJHG2h5ahpOOQw68+ThbyaJ1x9KNeJ61D6bHKMyExRY4vYJBPNDcCUA7Sv+Kzuj3PNjpvcW9GRBhRZ19ELF/mFkJqi7k07o1OVWUn11qrM2PIMmLOUdjPPLUsjxR6cAaYPIimTLvbn5Cqgvs6fpI1rIFUMBmB0xnRuyhWt3jQ1YvPVoVpy+HW0oU7hn5Dj7iEruQJfpj8aZfNBB2JFnGRt8oEQ+i5uaFZE1zfmM0VY4poQeEF/VOpotgpyBK8JS5YBhPLcp1ObEz8bmH0RuJhLM6tHd7q8iReaUvCvrykNve50H5clUhhuzmfxG4/ki+xhtAgMBAAECggEBAItQ/FPU+leuj1WgrykVV+5tKDgtCDE40S3uTQcF9zuoEN3qnNcsWuPafHwNC+7KlPfjs7KZ4x4TNPtxkq0eUc4LkAbOtwPhUw1tjCZBdNbFsK2iEH/7s6uzPLpiOeyB2Yz1VjUWRKbXMQbfa3tZ+41XgCoUeYhqJnqlNyq7QEr3BIIHRp64hi24C/EqpcNmuHpbFB27u2p03LIk+eOl2DhSJgLDEgK87bJc27PJCNLHr6jTknya78516b+g/QhCyI4VZRCSNMOT5D9XKQO05aHL9oEDNJFNTVUHW6kdy4TEAcOHVlO+I+nZS4xTPCh3VpUeEJ/RI9qUhS3ll3rFfk0CgYEA3hexe/z5K8R+iPWiTAcnTlispGq6ZOE3rnkBMc8Qly4a8jiK3qnYCx3u8IMxht5lrFPhZa/NJ4f4dqYaesFYuAmoX/MkUbe+ICw5UK+m7NE6GQFLC9Zs9h0tSpqkJMVuy9oNzBtePbY2yoZ9hUUZgCNBhvWNJ6rZ8wRMffF4OZ8CgYEArBZPfkyMxtxF+hCq/JmRe9/brAd85YzkbYeFOZhS5ly3edPKny9lV8B7Qd/6swvoLbvYFlgQ4VuZG3OeWqrlfye1NoF3Qv1qIwx8kO2d1ajxhFbrhgNP5YiGWAqhYEyQHjY9Orwbw3mjiMuhX08d9yaVl8CTw3c+uqRgXP+nCnMCgYB0D12otaJgxOZ7HCIcKQDZaG2P3lZZsXgvrPx60wKQfn+GZRXtFkTYSmQ7O9P97uqjC5KWWTCUn/Gvz023KKL8Sc+7lIw+SA8LMREioShSSWRa/NoC7aMzWjv46KcusB4eWW0sZIDzLu2jGHUv19RjqgjsRhXlzNrorC5tAmsZcQKBgCku3a9YGoisxoJl4sW46rw+go/U6ur0oOYlfJif/5bpsW8SZHdcHGC3y8ekFpcqzlHDlp8iTEfiZ+3N6aPBKlMX6aI1sZjylKf2oQj+FLvBRqtpMcxMlp5T4fLH+H7XU9YnHNgllG9feOeP8/qfMIipTzuV9sbcjsKT/AvFkvBJAoGAAZCaIWT62UZ3mk6ZndTwdhzecV2UQNbSaOliJd+6mJE2KUV88yQ0NW08nyntsXMgSNpseswIpYLiJCW/XB4hRWDHARS24XthLUYZDS3pC8MaLiepE2+4qmQ9PSjxEDVNFZSIbTjSBFQs+rb59hEvPuw55YdvEnKEmtgvpIEFYk8=";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAi1knalZEul1P9QP1tNE6eLDlunI18Rd5LLENYvsd5qciAGwBgcwgss0tSvRHs4RuzvFy0nyxcMTZS8rvCGsiLawLiVQz9xl6F500PaKGAhmzHYy2+Ys81AbQXp1SYo1ak8xozjrCmFS2/6uG4Abls2vu3JJf6855Eir57jqbR31vWbI3rOrHXnWg/uGrwNrBLlhcAJT+T42hzWgb69pbHOYqSLcSwPCUlxIrdj0kC3lTyKt+j6TpEMYz6hpkC5o949YiibdT+3wv3KdglzC5ZZkoFxgbcszmqaA5/pZVwyQ7xVxV3SGeC3kE7jdbKOnLl353wvuiVOnH2LengYwbRQIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url = "http://26g1yv49.dongtaiyuming.net/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url = "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    private String timeout = "30m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+timeout+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
